package com.csi.controller;


import com.csi.domain.UserBook;
import com.csi.service.OrderService;
import com.csi.utils.JsonResult;
import com.csi.utils.JwtUtil;
import com.csi.utils.ReturnResults;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单模块
 */
@RestController
@RequestMapping("/order")
@Tag(name = "order",description="订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 根据id查找订单
     * @param request
     * @return
     */
    @Operation(summary = "查找订单", description = "根据id和订单用户订单信息")
    @GetMapping("/findById")
    public Object findById(@Parameter (description = "订单状态")@RequestParam(value = "orderStatus",defaultValue = "-1",required = false) String orderStatus, @Parameter(description = "HTTP请求对象", required = true) HttpServletRequest request){
        String token =  request.getHeader("token");
        long userId = JwtUtil.getTokenBody(token).get("userId",Long.class);
        Map<String,Object> orders = orderService.findById(userId,orderStatus);
        if (orders != null ){
            return new JsonResult<>(orders);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查询失败");

    }

    @Operation(summary = "预定订单",description = "预定旅行套餐")
    @PostMapping("/bookOrder")
    public Object bookOrder(@Parameter(description = "用户预定信息")@RequestBody UserBook userBook,@Parameter(description = "HTTP请求对象", required = true) HttpServletRequest request){
        String token =  request.getHeader("token");
        long userId = JwtUtil.getTokenBody(token).get("userId",Long.class);

        userBook.setUserId(userId);
        int orderId = orderService.bookOrder(userBook);
        if (orderId != 0){
            return new JsonResult<>(orderId);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"预定失败");
    }


    @Operation(summary = "支付订单",description = "支付旅行套餐订单")
    @PostMapping("/payOrder")
    public Object payOrder(@Parameter(description = "预定订单id") @RequestParam("id") long payId){
//        String token =  request.getHeader("token");
//        long userId = JwtUtil.getTokenBody(token).get("userId",Long.class);

        int updated = orderService.updatedPayOrder(payId);
        if (updated > 0){
            return new JsonResult<>();
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"支付失败");

    }

    @Operation(summary = "完成订单",description = "线下核销更改订单状态为待评价")
    @PostMapping("/finishOrder")
    public Object finishOrder(@Parameter(description = "完成订单id") @RequestParam("id") long payId){
        int updated = orderService.updatedFinishOrder(payId);
        if (updated > 0){
            return new JsonResult<>();
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"核销失败");
    }

}
